package io.spring.guides.gs_producing_web_service;

import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @Author:wangshuliang
 * @Description:
 * @Date:Created in 14:56 2019/5/21
 * @Modified by:
 */
@Component
public class CountryRepository {
    private static final Map<String,Country> COUNTRY_MAP = new HashMap<>();
    @PostConstruct
    public  void initData(){
        Country country = new Country();
        country.setName("Spain");
        country.setCapital("Madrid");
        country.setCurrency(Currency.EUR);
        country.setPopulation(64704314);
        COUNTRY_MAP.put(country.getName(),country);

        Country poland = new Country();
        poland.setName("Poland");
        poland.setCapital("Warsaw");
        poland.setCurrency(Currency.PLN);
        poland.setPopulation(381868860);

        COUNTRY_MAP.put(poland.getName(),poland);

        Country uk = new Country();
        uk.setName("United Kingdom");
        uk.setCapital("London");
        uk.setCurrency(Currency.GBP);
        uk.setPopulation(63705000);

        COUNTRY_MAP.put(uk.getName(),uk);
    }

    public Country findCountry(String name){
        Assert.notNull(name,"The country's name must not be null");
        return COUNTRY_MAP.get(name);
    }
}
